﻿/**
 * 码内/间点击弹出窗口用JS插件
 * Dialog Window 
 * @author yanzai
 * 日期: 2015年3月10日 <br/>
 */
 var oUrl = null;
 var sletlinkType = null;
 var showFlag = true;
CKEDITOR.dialog.add( 'codejumpDialog', function( editor ) {
    return {
        title: '码内/间跳转',
        minWidth: 400,
        minHeight: 300,
        contents: [{
	        id: 'codejump',
	        label: 'code jump',
	        elements: [
	        	{
					id : "linkType",
					type : "select",
					label : '码跳转类型',					
					items : [['码内跳转', "codeInside"],
							 ['码间跳转', "codeBetween"]
							],
					"default" : "codeInside"
				},
	            {
	            	type: 'button',
				    id: 'buttonId',
				    label: '选 择',
				    title: '选 择',
				    onClick: function() {
				    	var dialog = this;
				    	oUrl = dialog.getDialog().getContentElement( 'codejump', 'url' ).getValue();
				    	sletlinkType = dialog.getDialog().getContentElement( 'codejump', 'linkType' ).getValue();
				    	
				    	var goodsBackFn = function(backGoods) {
						 	var dialogObj = dialog.getDialog();
						 	dialogObj.show()
						 	if(null != backGoods && backGoods[0].id){
							 	// 码间跳转：返回商品id
								var url = $("#mjtzUrl").val() + backGoods[0].id;
								dialogObj.once("show", function(){
									dialogObj.setupContent(url);
								});
								showFlag = false;
						 	}
						 };
						 var treeBackFn = function(backTree) {
						 	var dialogObj = dialog.getDialog();
						 	dialogObj.show()
						 	if(null != backTree && backTree.id){
							 	// 码内跳转：返回树节点id
							 	var url = $("#mntzUrl").val() + backTree.id;
							 	dialogObj.once("show", function(){
							 		dialogObj.setupContent(url);
							 	});
								showFlag = false;
						 	}
						 };
						 var closeObj = {closeFn : function(oUrl, sletlinkType) {
						 	var dialogObj = editor.openDialog( 'codejumpDialog');
						 	dialogObj.getContentElement( 'codejump', 'linkType' ).setValue(sletlinkType);
						 	if(null != oUrl){				 		
							 	var textObj = dialogObj.getContentElement( 'codejump', 'url' );
							 	textObj.setValue(oUrl);
						 	}
						 }, oUrl : oUrl, olinkType : sletlinkType};
						 var selectType = dialog.getDialog().getContentElement( 'codejump', 'linkType' ).getValue();
						 dialog.getDialog().hide();
						 if("codeInside" == selectType){
						 	// 码内跳转：选择树节点id
						 	var goodsId = $("#id").val();
						 	openTreeSelectWindow(goodsId, null, null, treeBackFn, closeObj);
						 } else if("codeBetween" == selectType){
						 	// 码间跳转：选择商品id
				         	openGoodsSelectWindow(goodsBackFn, null, true, null, null, null, closeObj);						 	
						 } else {
						 	// 码内跳转：选择树节点id
						 	var goodsId = $("#id").val();
						 	openTreeSelectWindow(goodsId, null, null, treeBackFn, closeObj);
						 }
				    }
	            },
	            {
	            	type: 'text',
				    id: 'url',
				    label: 'URL',
				    'default': '',
					setup : function(href) {
						// 必需要有 onShow 方法
						this.setValue(href);
					},
					commit : function(a) {
						a.url = this.getValue();
					},
				    validate: CKEDITOR.dialog.validate.notEmpty( "URL不允许为空！" )
	            }
	        ]
	    }],
		onShow : function() {
			var a = this.getParentEditor(), b = a.getSelection(), c = null;
			(c = CKEDITOR.plugins.link.getSelectedLink(a)) && c.hasAttribute("href") ? b.getSelectedElement()
					|| b.selectElement(c) : c = null;
			a = CKEDITOR.plugins.link.parseLinkAttributes(a, c);
			this._.selectedElement = c;
			if (c) {
				this.setupContent(c.getAttribute("href"));
			} else {
				this.setupContent("");
			}
		},
       	onOk: function() {
            var dialog = this;
			var url = {};
			dialog.commitContent(url);
			debugger;
			var aLink = CKEDITOR.plugins.link.getSelectedLink(editor);
			if (aLink) {
				aLink.$.href = url.url;
			}
			var selectedRag = editor.getSelection().getRanges()[0];
			var link = new CKEDITOR.style({
				element: 'a',
				attributes: {
					"href" : url.url
				}
			});
			link.type = CKEDITOR.STYLE_INLINE;
			link.applyToRange(selectedRag, editor);
			selectedRag.select();
        }
    };
});
 